﻿using AnQinManagermentBackendAPI.Common.DateBase;
using AnQinManagermentBackendAPI.Common.Request;
using AnQinManagermentBackendAPI.Model.DTO.Customer;
using AnQinManagermentBackendAPI.Model.Entity;
using AnQinManagermentBackendAPI.Model.View.Customer;
using Microsoft.EntityFrameworkCore;

namespace AnQinManagermentBackendAPI.Service
{
    public class CustomerService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 构造注入
         */
        public CustomerService(DataContext context)
        {
            _context = context;
        }

        /**
         * 获取客户列表
         */
        public async Task<RequestResponseModel<PageResult<CustomerView>>> GetCustomerList(CustomerSearchDTO customerSearchDTO)
        {
            //筛选
            var query = _context.CustomerView
                .WhereIF(a => a.Code.Contains(customerSearchDTO.Code), !string.IsNullOrEmpty(customerSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(customerSearchDTO.Name), !string.IsNullOrEmpty(customerSearchDTO.Name))
                .WhereIF(a => a.Contact.Contains(customerSearchDTO.Contact), !string.IsNullOrEmpty(customerSearchDTO.Contact))
                ;

            //返回列表
            var customerList = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(customerSearchDTO.CurrentPage, customerSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<CustomerView>(customerList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增客户
         */
        public async Task<RequestResponseModel<string>> AddCustomer(CustomerAddDTO customerAddDTO)
        {
            //操作数据
            DateTime dateTime = DateTime.Now;
            Guid guid = Guid.NewGuid();

            //生成新数据
            var customer = new CustomerModel
            {
                Id = guid.ToString(),
                Code = customerAddDTO.Code,
                Name = customerAddDTO.Name,
                Sex = customerAddDTO.Sex,
                Age = customerAddDTO.Age,
                Birthday = customerAddDTO.Birthday,
                Contact = customerAddDTO.Contact,
                Wechat = customerAddDTO.Wechat,
                Email = customerAddDTO.Email,
                CreateTime = dateTime
            };

            //新增客户
            _context.Customer.Add(customer);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "新增成功", guid.ToString());
        }

        /**
         * 编辑客户
         */
        public async Task<RequestResponseModel<string>> PutCustomer(CustomerUpdateDTO customerUpdateDTO)
        {
            //查找数据
            var query = await _context.Customer.SingleOrDefaultAsync(a => a.Id == customerUpdateDTO.Id);

            //校验数据
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", customerUpdateDTO.Id);
            };

            //生成新数据
            query.Name = customerUpdateDTO.Name;
            query.Age = customerUpdateDTO.Age;
            query.Sex = customerUpdateDTO.Sex;
            query.Birthday = customerUpdateDTO.Birthday;
            query.Contact = customerUpdateDTO.Contact;
            query.Wechat = customerUpdateDTO.Wechat;
            query.Email = customerUpdateDTO.Email;

            //更新客户表
            _context.Customer.Update(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", customerUpdateDTO.Id);
        }

        /**
         * 删除客户
         */
        public async Task<RequestResponseModel<string>> DeleteCustomer(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.Customer.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //删除操作
            _context.Customer.Remove(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 删除客户 - 批量
         */
        public async Task<RequestResponseModel<List<string>>> DeleteCustomerMulti(List<string> Ids)
        {
            //判空
            if (Ids == null || Ids.Count == 0)
            {
                return RequestResponse.CreateResponse(400, "Ids为空", new List<string>());
            }

            //查找id
            var query = await _context.Customer
                .Where(a => Ids.Contains(a.Id))
                .ToListAsync();

            //是否有匹配资源
            if (query.Count == 0)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Ids);
            }

            //操作数据库
            _context.Customer.RemoveRange(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Ids);
        }
    }
}
